package leetcode;

import java.util.Arrays;

public class LeetCode80 {

    public static void main(String[] args) {
        LeetCode80 leetCode80 = new LeetCode80();
        System.out.println(leetCode80.removeDuplicates(new int[]{1, 1, 1, 2, 2, 3}));
    }

    public int removeDuplicates(int[] nums) {
        int writeIndex = 0, readIndex = 0, tmpIndex, num;
        while (readIndex != nums.length) {
            num = nums[readIndex];
            tmpIndex = readIndex;
            while (tmpIndex < nums.length && num == nums[tmpIndex])
                tmpIndex++;
            if (tmpIndex - readIndex > 1) {
                nums[writeIndex++] = num;
            }
            nums[writeIndex++] = num;
            readIndex = tmpIndex;
        }
        return writeIndex;
    }
//------------------------------------------------------------------
}
